package com.google.code.netbuilder.services.equipment;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import com.google.code.netbuilder.dal.entities.Equipment;
import com.google.code.netbuilder.dal.entities.EquipmentTemplate;
import com.google.code.netbuilder.dal.providers.IEquipmentProvider;
import com.google.code.netbuilder.dal.providers.IEquipmentTemplateProvider;
import com.google.code.netbuilder.dal.providers.IProviderManager;

/**
 * Creates and saves equipment with entered address according to entered template name
 * @author Sergey Kozlov
 */
@ManagedBean(name = "equipmentCreationService")
@SessionScoped
public class EquipmentCreationService {

//      @EJB
        private IEquipmentProvider ep;
        
        @EJB
        private IProviderManager pm;
        
        @EJB
        private IEquipmentTemplateLoader eqTLoader;
        
        @EJB
        private IEquipmentTemplateProvider etp;
        
        private Equipment currentEquipment;
        
        private Equipment resEquipment;
        
        private String templateName;
        
        private List<String> templateNames;

        @PostConstruct
        public void afteInitialize(){
                System.out.println("**********************************************************************");
                ep = (IEquipmentProvider) pm.getProvider("Equipment", "JPA");
                System.out.println("**********************************************************************");
                currentEquipment = new Equipment();
                resEquipment = new Equipment(); 
        }
        
        
        public List<String> getTemplateNames() {
            List<EquipmentTemplate> templates;
            templates = etp.findAll();
            templateNames = new ArrayList<String>();
            for(EquipmentTemplate et: templates){
                templateNames.add(et.getName());
            }
            return templateNames;
        }


        public void setTemplateNames(List<String> templateNames) {
                this.templateNames = templateNames;
        }

        public Equipment getCurrentEquipment() {
                return currentEquipment;
        }

        public void setCurrentEquipment(Equipment currentEquipment) {
                this.currentEquipment = currentEquipment;
        }
        
        public String getTemplateName() {
                return templateName;
        }

        public void setTemplateName(String templateName) {
                this.templateName = templateName;
        }

        public void createEquipment(){  
                resEquipment = eqTLoader.loadEquipmentFromTemplateName(templateName);
                resEquipment.setAddress(currentEquipment.getAddress());
                resEquipment.setName(currentEquipment.getName());
                System.out.println("Creating: " + resEquipment.toString());
                ep.save(resEquipment);
                addMessage("Equipment " + resEquipment.getName() + " saved");
        }
        
        public void addMessage(String summary) {  
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, summary,  null);  
            FacesContext.getCurrentInstance().addMessage(null, message);  
        }
}